package com.atguigu.ggkt.vod.controller;


import com.atguigu.ggkt.model.vod.Teacher;
import com.atguigu.ggkt.result.Result;
import com.atguigu.ggkt.vo.vod.TeacherQueryVo;
import com.atguigu.ggkt.vod.service.TeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author henry
 * @since 2023-02-14
 */
@Api(tags = "讲师管理接口")
@RestController
@RequestMapping(value="/admin/vod/teacher")
//@CrossOrigin //跨域
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

  /*  @ApiOperation("查询所有讲师")
    //查询所有讲师
    @GetMapping("findAll")
    public List<Teacher> findAllTeacher(){
        List<Teacher> list = teacherService.list();
        return list;
    }*/

    @ApiOperation("查询所有讲师")
    //查询所有讲师
    @GetMapping("findAll")
    public Result findAllTeacher(){
        List<Teacher> list = teacherService.list();
        return Result.ok(list).message("查询数据成功");
    }

    @ApiOperation("逻辑删除讲师")
    //逻辑删除讲师
    @DeleteMapping("remove/{id}")
    public Result removeTeacher(@ApiParam(name = "id", value = "ID", required = true)@PathVariable Long id){
        boolean isSuccess = teacherService.removeById(id);
        if (isSuccess){
            return Result.ok(null);
        }else {
            return Result.fail(null);
        }
    }

    //条件查询分页
    @ApiOperation("条件查询分页")
    @PostMapping("findQueryPage/{current}/{limit}")
    public Result findPage(@PathVariable long current,
                           @PathVariable long limit,
                           @RequestBody(required = false) TeacherQueryVo teacherQueryVo){

        Page<Teacher> pageParam = new Page<>(current,limit);
        //判断teacherQueryVo对象是否为空
        if (teacherQueryVo == null){
            Page<Teacher> page = teacherService.page(pageParam,null);
            return Result.ok(page);
        }else{
            //获取条件值
            String name = teacherQueryVo.getName();
            Integer level = teacherQueryVo.getLevel();
            String joinDateBegin = teacherQueryVo.getJoinDateBegin();
            String joinDateEnd = teacherQueryVo.getJoinDateEnd();

            //进行非空的判断，条件封装
            QueryWrapper<Teacher> wrapper =  new QueryWrapper<>();
            if (!StringUtils.isEmpty(name)){
                wrapper.like("name" , name);
            }

            if (!StringUtils.isEmpty(level)){
                wrapper.eq("level" , level);
            }

            if (!StringUtils.isEmpty(joinDateBegin)){
                wrapper.ge("join_data" , joinDateBegin);
            }
            if (!StringUtils.isEmpty(joinDateEnd)){
                wrapper.le("join_data" , joinDateEnd);
            }

            //调用方法分页查询

            Page<Teacher> page = teacherService.page(pageParam, wrapper);

            return  Result.ok(page);
        }

    }



    //4 添加讲师
    @ApiOperation("添加讲师")
    @PostMapping("saveTeacher")
    public Result saveTeacher(@RequestBody Teacher teacher){
        boolean isSuccess = teacherService.save(teacher);
        if (isSuccess){
            return Result.ok(null);
        }else {
            return Result.fail(null);
        }

    }

    //5 修改- 根据id查询
    @ApiOperation("根据id查询")
    @GetMapping("getTeacher/{id}")
    public Result getTeacher(@PathVariable Long id)
    {
        Teacher byId = teacherService.getById(id);
        return Result.ok(byId);
    }

    // 6 修改的最终实现
    @ApiOperation("修改最终实现")
    @PostMapping("updateTeacher")
    public Result updateGTeacher(@PathVariable Teacher teacher){
        boolean isSuccess = teacherService.updateById(teacher);
        if (isSuccess){
            return Result.ok(null);
        }else {
            return Result.fail(null);
        }

    }

    //7 批量删除讲师
    //json数组[1,2,3]
    @ApiOperation("批量删除讲师")
    @DeleteMapping("removeBatch")
    public Result removeBatch(@RequestBody List<Long> idList){

        boolean isSuccess = teacherService.removeByIds(idList);
        if (isSuccess){
            return Result.ok(null);
        }else {
            return Result.fail(null);
        }

    }

    @ApiOperation("根据id查询")
    @GetMapping("inner/getTeacher/{id}")
    public Teacher getTeacherInfo(@PathVariable Long id)
    {
        Teacher teacher = teacherService.getById(id);
        return teacher;
    }


}

